//
//  ZDCommitController.m
//  ZDPension
//
//  Created by 刘甲奇 on 2017/6/21.
//  Copyright © 2017年 maker. All rights reserved.
//

#import "ZDCommitController.h"
#import "ZDScoreCell.h"
#import "ZDGoodsScoreCell.h"

static NSString *const ZDScoreCellId = @"ZDScoreCell";
static NSString *const ZDGoodsScoreCellId = @"ZDGoodsScoreCell";

@interface ZDCommitController ()

/**
 socre
 */
@property (nonatomic, assign) NSInteger totalScore;
/**
 服务与秒速
 */
@property (nonatomic, assign) NSInteger descriptionScore;
/**
 商家态度
 */
@property (nonatomic, assign) NSInteger atitudeScore;
/**
 商家速度
 */
@property (nonatomic, assign) NSInteger speedScore;
/**
 评价的内容
 */
@property(nonatomic, copy) NSString *commitContent;
/**
 <#Description#>
 */
@property(nonatomic, strong) NSArray *titleArray;

/**
 订单中一共有多少个商品
 */
@property(nonatomic, assign)  NSInteger goodsNumber;

/**
 评价完的数组
 */
@property(nonatomic, strong) NSMutableArray *commitArray;



@end

@implementation ZDCommitController

- (void)setupUI
{
    [super setupUI];
    self.title = @"去评价";
    
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightButton setTitle:@"评价" forState:0];
    rightButton.titleLabel.font = [UIFont systemFontOfSize:15];
    rightButton.frame = CGRectMake(0, 0, 40, 30);
    [rightButton addTarget:self action:@selector(rightClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"ZDScoreCell" bundle:nil] forCellReuseIdentifier:ZDScoreCellId];
    [self.tableView registerNib:[UINib nibWithNibName:@"ZDGoodsScoreCell" bundle:nil] forCellReuseIdentifier:ZDGoodsScoreCellId];
}
- (void)loadData
{
    self.titleArray = @[@"服务与描述相符",@"店家的服务态度",@"店家的服务速度"];
    self.goodsNumber = self.orderModel.extend_order_goods.count;
    
    self.descriptionScore = 5;
    self.atitudeScore = 5;
    self.speedScore = 5;
    
}
- (void)requstAddCommit
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:@(self.atitudeScore) forKey:@"store_servicecredit"];
    [dict setObject:@(1) forKey:@"anony"];
    [dict setObject:@(self.descriptionScore) forKey:@"store_desccredit"];
    
    NSMutableString *content = [NSMutableString string];
    for (int i = 0; i < self.commitArray.count; i++) {
        ZDGoodsDetailModel *model = self.commitArray[i];
        if (i == self.commitArray.count) {
            [content appendFormat:@"%@|%@|%@",model.goods_id,model.score,model.commitContent];
        }else{
            [content appendFormat:@"%@|%@|%@,",model.goods_id,model.score,model.commitContent];
        }
    }
    [dict setObject:content forKey:@"goods"];
    [dict setObject:@(self.speedScore) forKey:@"store_deliverycredit"];
    [dict setObject:self.orderModel.order_id forKey:@"order_id"];
    
    [SVProgressHUD showWithStatus:@"评论中"];
    __weak typeof (self) weakSelf = self;
    [API requestPostAppendKey:submitCommitUrl params:dict success:^(NSDictionary *successCode) {
        [SVProgressHUD dismiss];
        ZDLog(@"%s---%d---%@",__func__,__LINE__,successCode);
        NSDictionary *datas = successCode[@"datas"];
        NSString *error = datas[@"error"];
        if (error)
        {
            [SVProgressHUD showInfoWithStatus:error];
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }else
        {
            [SVProgressHUD showInfoWithStatus:@"评论成功"];
            [weakSelf.navigationController popViewControllerAnimated:YES];
            [[NSNotificationCenter defaultCenter] postNotificationName:COMMIT_SUCCESS_NOTIFICATION object:nil];
        }
        
    } falure:^(NSError *er) {
        ZDLog(@"%s---%d---%@",__func__,__LINE__,er);
        [SVProgressHUD dismiss];
    }];

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.orderModel.extend_order_goods.count + 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger count = self.orderModel.extend_order_goods.count;
    if (indexPath.row >= count)
    {
        ZDScoreCell *cell = [tableView dequeueReusableCellWithIdentifier:ZDScoreCellId forIndexPath:indexPath];
        
        __weak typeof (self) weakSelf = self;
        [cell scoreCellClick:^(NSInteger score) {
            [weakSelf handleScore:indexPath Score:score];
        }];
        cell.titleLabel.text = self.titleArray[indexPath.row -count];
        return cell;
    }
    
    ZDGoodsScoreCell *cell = [tableView dequeueReusableCellWithIdentifier:ZDGoodsScoreCellId forIndexPath:indexPath];
    ZDGoodsDetailModel *detailModel = self.orderModel.extend_order_goods[indexPath.row];
    cell.detailModel = detailModel;
     __weak typeof (self) weakSelf = self;
    [cell goodsScoreClick:^(ZDGoodsDetailModel *model) {
        ZDLog(@"%s---%d---%ld conten = %@",__func__,__LINE__,indexPath.row,model.commitContent);
        if (weakSelf.commitArray.count>= _goodsNumber)
        {
            [weakSelf.commitArray replaceObjectAtIndex:indexPath.row withObject:model];
        }else
        {
            if (indexPath.row>weakSelf.commitArray.count)
            {
                [weakSelf.commitArray addObject:model];
            }else{
                [weakSelf.commitArray insertObject:model atIndex:indexPath.row];
            }
        }
        
    }];
    return cell;
}

#pragma mark- 点击事件
- (void)rightClick
{
    ZDLog(@"%s---%d---评价",__func__,__LINE__);
    //发起评价的请求
    if (self.commitArray.count<_goodsNumber) {
        [SVProgressHUD showInfoWithStatus:@"评价完商品再提交"];
        return;
    }else{
        //遍历内容
        for (ZDGoodsDetailModel *model in self.commitArray) {
            if (model.commitContent.length == 0)
            {
                [SVProgressHUD showInfoWithStatus:@"填写下评价内容"];
                return;
            }
        }
    }
    
    [self requstAddCommit];
}
- (void)handleScore:(NSIndexPath *)indexPath Score:(NSInteger)score
{
    switch ((indexPath.row -_goodsNumber)) {
        case 0:
            self.descriptionScore = score;
            break;
        case 1:
            self.atitudeScore = score;
            break;
        case 2:
            self.speedScore = score;
            break;
            
        default:
            break;
    }
}
- (NSMutableArray *)commitArray
{
    if (_commitArray == nil) {
        _commitArray = [NSMutableArray array];
    }
    return _commitArray;
}
@end
